package com.squirrel.michale;

import java.util.ArrayList;
import java.util.List;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/22 7:15 PM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode0054 {

    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> spiralOrder = new ArrayList<>();
        int row = matrix.length;
        int column = matrix[0].length;
        int left = 0;
        int right = column - 1;
        int up = 0;
        int down = row - 1;
        while (true) {
            for (int i = left; i <= right; i++) {
                spiralOrder.add(matrix[up][i]);
            }
            up++;
            if (up > down) {
                break;
            }
            for (int i = up; i <= down; i++) {
                spiralOrder.add(matrix[i][right]);
            }
            right--;
            if (right < left) {
                break;
            }
            for (int i = right; i >= left ; i--) {
                spiralOrder.add(matrix[down][i]);
            }
            down--;
            if (down < up) {
                break;
            }
            for (int i = down; i >= up; i--) {
                spiralOrder.add(matrix[i][left]);
            }
            left++;
            if (right < left) {
                break;
            }
        }
        return spiralOrder;
    }


    public List<Integer> spiralOrder2(int[][] matrix) {

        List<Integer> spiralOrder = new ArrayList<>();

        int row = matrix.length;
        int column = matrix[0].length;

        for (int j = 0; j < column; j++) {

        }

        for (int i = 0; i < column; i++) {
            spiralOrder.add(matrix[0][i]);
        }

        for (int i = 1; i < row; i++) {
            spiralOrder.add(matrix[i][column]);
        }

        for (int i = column-2; i > 0; i--) {
            spiralOrder.add(matrix[row][i]);
        }

        for (int i = row-2; i > 0; i--) {
            spiralOrder.add(matrix[i][0]);
        }

        for (int i = 0; i < column-2; i++) {
            spiralOrder.add(matrix[1][i]);
        }
        return new ArrayList<>();
    }

    public static void main(String[] args) {
//        Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
//        Output: [1,2,3,4,8,12,11,10,9,5,6,7]

        LeetCode0054 leetCode0054 = new LeetCode0054();

//        int[][] matrix = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
//        List<Integer> integerList = leetCode0054.spiralOrder(matrix);
//        for (int i = 0; i < integerList.size(); i++) {
//            System.out.println(integerList.get(i));
//        }


//        int[][] matrix2 = new int[][]{{7}, {9}, {6}};
//        List<Integer> integerList2 = leetCode0054.spiralOrder(matrix2);
//        for (int i = 0; i < integerList2.size(); i++) {
//            System.out.println(integerList2.get(i));
//        }


//        输入
//                matrix =
//[[2,5,8],[4,0,-1]]
//        19 / 23 个通过的测试用例
//                输出
//[2,5,8,-1,0,4,0]
//        预期结果
//                [2,5,8,-1,0,4]

        int[][] matrix3 = new int[][]{{2,5,8},{4,0,-1}};
        List<Integer> integerList3 = leetCode0054.spiralOrder(matrix3);
        for (int i = 0; i < integerList3.size(); i++) {
            System.out.println(integerList3.get(i));
        }


    }
}
